﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{

    public partial class YCAAV25Data : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        Queue<DataSet> JobQueue;
        DataSet Set4ITX = new DataSet();
        DataRow dwIntexStd = null;
        string STKNR = "             ";
        int WLFDNR = 1;
        string FCCHAR = "";
        bool IsRun = false;
        string XFBLO = "", XFBLB = "", XFBLP = "";
        string PRLO = "", PRLB = "", PRLP = "";
        string processtype = "";
        string time;
        string WBEWBELEG;
        string WBEWBUCHUHR;
        public DataSet SetMap;
        bool IsSuccess = true;
        string message = "";
        public Convert2Intex Convert2Intex;
        public DataSet SetExt = new System.Data.DataSet();
        public DataSet SetJob;
        YCAAUTOAB2INTEX m_wintex;
        BackgroundWorker bgWorker;
        public YCAAV25Data()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
            JobQueue = new Queue<DataSet>();
            bgWorker = new BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
        }

        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IsRun = false;
        }
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (!IsRun)
            {
                IsRun = true;
            }
            AutoRun();
        }
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = INTEXDB.LIEFDAT.NewLIEFDATRow();
            dwData["LSMANDANT"] = "00";
            SResult = INTEXDBC.QueryLIEFDAT(dwData, 8);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                rx["LSNAME1"] = Helper.GetGB2312Value(rx["LSNAME1"].ToString().Trim());
            }
            SReturn.Merge(SResult);
            SetMap = new DataSet();
            DataSet STemp = new System.Data.DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\ABFBMAPINTEX.XML");
            STemp.ReadXml(filePath);
            SetMap.Merge(STemp);
            return SReturn;
        }
        public DataSet ABPLoad(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
                DataRow dwData = INTEXDB.ABPOS.NewRow();
                dwData["ABPMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABPART"] = dwInput["ABPART"];
                SResult = INTEXDBC.QueryABPOS(dwData, 17);
                SResult.Tables[0].TableName = "ABPAUF";
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet LoadABPOS(DataRow dwInput)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
                DataRow dwData = INTEXDB.ABPOS.NewRow();
                dwData = INTEXDB.ABPOS.NewRow();
                dwData["ABPMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryABPOS(dwData, 18);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ABFOLGE.NewRow();
                dwData["ABFMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABFAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABFAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryABFOLGE(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ABPOS.NewRow();
                dwData["ABPMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryFSSTAMM(dwData, 8);
                SReturn.Merge(SResult);

                dwData = INTEXDB.ABPOS.NewRow();
                dwData["ABPMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryFSSTAMM(dwData, 8);
                SReturn.Merge(SResult);
                /*dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABAVKAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABAVKAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryICC_ABAUFM(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = dwInput["ABPMANDANT"];
                dwData["ABAVKAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABAVKAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryEKPOS(dwData, 3);
                SReturn.Merge(SResult);*/
                dwData = INTEXDB.ABPOS.NewRow();
                dwData["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
                dwData["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryICC_ABPOS_EXT(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ICC_ABPOS_MAT.NewRow();
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                SResult = INTEXDBC.QueryICC_ABPOS_MAT(dwData, 5);
                if (SResult.Tables.Count > 0)
                {
                    foreach (DataRow row in SResult.Tables[0].Rows)
                    {
                        row["SY"] = 0;
                        row["MY"] = 0;
                        row["LY"] = 0;
                        row["XLY"] = 0;
                        decimal qexist = 0;
                        decimal qty = 0;
                        if (!SReturn.Tables.Contains("ICC_ABPOS_EXT"))
                            continue;
                        if (SReturn.Tables["ICC_ABPOS_EXT"].Rows.Count > 0)
                        {
                            System.Windows.MessageBox.Show("已经执行入库，莫重复");
                            continue;
                        }
                        var query = from DataRow ra in SReturn.Tables["ICC_ABPOS_EXT"].AsEnumerable()
                                    where ra["ABPAUFKRS"].ToString() == row["KRS"].ToString()
                                    && ra["ABPAUFLFD"].ToString() == row["LFD"].ToString()
                                    && ra["ABPFARBE"].ToString() == row["ABPFARBE"].ToString()
                                    select ra;
                        foreach (DataRow RX in query)
                        {
                            switch (RX["ABPGROE"].ToString().Trim())
                            {
                                case "S":
                                    qexist = Convert.ToDecimal(row["SY"].ToString());
                                    qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                    row["SY"] = qexist + qty;
                                    break;
                                case "M":
                                    qexist = Convert.ToDecimal(row["MY"].ToString());
                                    qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                    row["MY"] = qexist + qty;
                                    break;
                                case "L":
                                    qexist = Convert.ToDecimal(row["LY"].ToString());
                                    qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                    row["LY"] = qexist + qty;
                                    break;
                                case "XL":
                                    qexist = Convert.ToDecimal(row["XLY"].ToString());
                                    qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                    row["XLY"] = qexist + qty;
                                    break;
                            }
                        }
                    }
                }
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet LoadExisting()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
                string processd = "";
                string KRS, LFD;
                DataRow dwData = INTEXDB.ABPOS.NewRow();

                SResult = INTEXDBC.QueryICC_ABPOS_EXT(null, 99);
                SResult.Tables[0].TableName = "ABPOS";
                SReturn.Merge(SResult);
                dwData = INTEXDB.ICC_ABPOS_MAT.NewRow();
                foreach (DataRow rx in SResult.Tables[0].Rows)
                {
                    KRS = rx["ABPAUFKRS"].ToString();
                    LFD = rx["ABPAUFLFD"].ToString();
                    if (processd.Contains(KRS + LFD + ";"))
                    {
                        continue;
                    }
                    processd += KRS + LFD + ";";
                    dwData["KRS"] = rx["ABPAUFKRS"];
                    dwData["LFD"] = rx["ABPAUFLFD"];
                    DataSet STemp = INTEXDBC.QueryICC_ABPOS_MAT(dwData, 5);
                    foreach (DataRow row in STemp.Tables[0].Rows)
                    {
                        row["ABPAUFART"] = rx["ABPAUFART"];
                        row["SY"] = 0;
                        row["MY"] = 0;
                        row["LY"] = 0;
                        row["XLY"] = 0;

                    }
                    SReturn.Merge(STemp);
                }
                foreach (DataRow row in SReturn.Tables["ICC_ABPOS_MAT"].Rows)
                {
                    KRS = row["KRS"].ToString();
                    LFD = row["LFD"].ToString();
                    string ABPFARBE = row["ABPFARBE"].ToString();
                    decimal qexist = 0;
                    decimal qty = 0;
                    var query = from DataRow ra in SReturn.Tables["ABPOS"].AsEnumerable()
                                where ra["ABPAUFKRS"].ToString() == KRS
                                && ra["ABPAUFLFD"].ToString() == LFD
                                && ra["ABPFARBE"].ToString() == ABPFARBE
                                select ra;
                    foreach (DataRow RX in query)
                    {
                        switch (RX["ABPGROE"].ToString().Trim())
                        {
                            case "S":
                                qexist = Convert.ToDecimal(row["SY"].ToString());
                                qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                row["SY"] = qexist + qty;
                                break;
                            case "M":
                                qexist = Convert.ToDecimal(row["MY"].ToString());
                                qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                row["MY"] = qexist + qty;
                                break;
                            case "L":
                                qexist = Convert.ToDecimal(row["LY"].ToString());
                                qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                row["LY"] = qexist + qty;
                                break;
                            case "XL":
                                qexist = Convert.ToDecimal(row["XLY"].ToString());
                                qty = Convert.ToDecimal(RX["ABPEAKG"].ToString());
                                row["XLY"] = qexist + qty;
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet LoadData(DataRow dwInput)
        {

            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            try
            {
                DataRow dwData = INTEXDB.WDECK.NewRow();
                dwData["WDMANDANT"] = "00";
                dwData["WDAUFKRS"] = dwInput["KRS"];
                dwData["WDAUFLFD"] = dwInput["LFD"];
                SResult = INTEXDBC.QueryWDECK(dwData, 19);
                SReturn.Merge(SResult);
                dwData = INTEXDB.FDECK.NewRow();
                dwData["FDMANDANT"] = "00";
                dwData["FDAUFKRS"] = dwInput["KRS"];
                dwData["FDAUFLFD"] = dwInput["LFD"];
                SResult = INTEXDBC.QueryFDECK(dwData, 19);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ABFOLGE.NewRow();
                dwData["ABFMANDANT"] = "00";
                dwData["ABFAUFKRS"] = dwInput["KRS"];
                dwData["ABFAUFLFD"] = dwInput["LFD"];

                SResult = INTEXDBC.QueryABFOLGE(dwData, 2);
                SReturn.Merge(SResult);
                dwData = INTEXDB.ABAUFM.NewRow();
                dwData["ABAMANDANT"] = "00";
                dwData["ABAAUFKRS"] = dwInput["KRS"];
                dwData["ABAAUFLFD"] = dwInput["LFD"];
                SResult = INTEXDBC.QueryABAUFM(dwData, 2);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        #region======Run===========
        public DataSet LoadJob()
        {
            DataSet SReturn = new DataSet();
            try
            {
                bool last = true;
                DataSet SResult = GetJob(ref last);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet GetJob(ref  bool last)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();

            try
            {
                string fileDone;
                string filestemp = @"C:\IntexReport\Data\YCAAV25\01Temp";
                string[] files = System.IO.Directory.GetFiles(filestemp);
                foreach (string file in files)
                {
                    fileDone = file.Replace(@"\01Temp\", @"\02Incoming\");
                    System.IO.File.Move(file, fileDone);
                }
                string files2p = @"C:\IntexReport\Data\YCAAV25\02Incoming";
                files = System.IO.Directory.GetFiles(files2p);

                if (files.Length > 0)
                {
                    string file = files[0];
                    SResult.ReadXml(file);
                    SReturn.Merge(SResult);
                    fileDone = file.Replace(@"\02Incoming\", @"\03Done\");
                    System.IO.File.Move(file, fileDone);
                }
                if (files.Length == 1)
                    last = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
            return SReturn;
        }
        public void SaveJob()
        {
            DataSet Set = LoadJob();
            if (Set.Tables.Count > 0)
            {
                JobQueue.Enqueue(Set);
            }
            else
            {
                return;
            }
            if (IsRun)
            {
                return;
            }
            if (!IsRun)
            {
                bgWorker.RunWorkerAsync();
            }
        }
        public void AutoRun()
        {

            try
            {
                while (true)
                {
                    if (JobQueue.Count > 0)
                    {
                        SetJob = JobQueue.Dequeue();
                        ConvertToIntex();
                        SaveJob();//loadjob
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                IsRun = false;
                Tracer.Error(ex.StackTrace, ex);
                EventDispatcher.Instance.RaiseStatus("完成但是暂停了任务", StatusEventType.StopProgress);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }

        public bool Upload2Intex()
        {
            bool result = true;
            try
            {
                if (m_wintex == null)
                {
                    m_wintex = new YCAAUTOAB2INTEX();
                }
                m_wintex.ReSet(Set4ITX, null);
                m_wintex.Execute(null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
            return result;
        }
        public DataSet ConvertToIntex()
        {
            WBEWBELEG = INTEXDBC.GetAUFLFD("  ", "BEWEGUNG");
            DataSet Result = new DataSet();
            string ART = "", Color = "";
            DataSet Return = new DataSet();//is used to magage the data to upload to intex 
            string krs = "", lfd = "", pos = "";
            foreach (DataRow dwABPOS_EXT in SetJob.Tables["ABPOS_EXT"].Rows)
            {
                message = "";
                processtype = "";
                if (dwABPOS_EXT["ABPAUFART"].ToString() == "RP")
                {
                    processtype = dwABPOS_EXT["ABPAUFART"].ToString();
                    Repair(dwABPOS_EXT);
                    continue;
                }

                try
                {
                    ART = dwABPOS_EXT["ABPART"].ToString().Trim();
                    Color = dwABPOS_EXT["ABPFARBE"].ToString().Trim();
                    Set4ITX.Clear();
                    SetExt.Clear();
                    SetExt.Merge(INTEXDB);
                    SetExt.Merge(SetMap);
                    Convert2Intex = new Convert2Intex(SetExt, dwIntexStd, OracleDB, proxy);
                    dwIntexStd = Convert2Intex.dwIntexStd;
                    dwIntexStd["ITXUSER"] = UserID;
                    dwIntexStd["ITXDATE"] = date;
                    #region======Prepare=========
                    INTEXDBC.SaveICC_ABPOS_EXT(dwABPOS_EXT);

                    DataRow dwData = null;
                    DataRow dwABPOS = null;
                    krs = dwABPOS_EXT["ABPAUFKRS"].ToString();
                    lfd = dwABPOS_EXT["ABPAUFLFD"].ToString();
                    pos = dwABPOS_EXT["ABPAUFPOS"].ToString();
                    dwData = INTEXDB.ICC_ABAUFM.NewRow();
                    dwData["ABAMANDANT"] = dwABPOS_EXT["ABPMANDANT"];
                    dwData["ABAVKAUFKRS"] = krs;
                    dwData["ABAVKAUFLFD"] = lfd;
                    dwData["ABAVKAUFPOS"] = pos;
                    Result = INTEXDBC.QueryEKPOS(dwData, 4);
                    EventDispatcher.Instance.RaiseStatus("开始检查采购单" + krs + lfd, StatusEventType.RunningProgress);
                    foreach (DataRow row in Result.Tables[0].Rows)
                    {
                        decimal EKPME1 = Convert.ToDecimal(row["EKPME1"].ToString());
                        decimal EKPME3 = Convert.ToDecimal(row["EKPME3"].ToString());
                        if (EKPME3 == 0)
                        {
                            IsSuccess = false;
                            message += message += krs + lfd + "-" + row["EKPAUFKRS"].ToString() + "-" + Helper.GetSubRollNum(row["EKPAUFLFD"].ToString()) +
                                row["ABAART"].ToString().Trim() + "-" + row["ABAFARBE"].ToString().Trim() + "-" + row["ABAGROE"].ToString().Trim() +
                                 ":,请先入库!";
                            INTEXBL.CheckEvents("A003001", dwABPOS_EXT, message, null);
                            EventDispatcher.Instance.RaiseStatus(message, StatusEventType.StopProgress);
                            throw new Exception(message);
                        }
                    }
                    EventDispatcher.Instance.RaiseStatus("核对完毕采购单" + krs + lfd, StatusEventType.RunningProgress);
                    decimal nQty = System.Convert.ToDecimal(dwABPOS_EXT["ABPEAKG"]);
                    if (nQty == 0)
                    {
                        if (dwABPOS_EXT["ABPERLDAT"].ToString() == date)
                        {
                            dwABPOS_EXT["ABPAENDBEN"] = UserID;
                            dwABPOS_EXT["ABPAENDDAT"] = date;
                            INTEXDBC.UpdateABPOS(dwABPOS_EXT);
                        }
                        continue;
                    }
                    DateTime now = System.DateTime.Now;
                    WBEWBUCHUHR =
                        Right("00" + now.Hour.ToString(), 2) +
                        Right("00" + now.Minute.ToString(), 2) +
                        Right("00" + now.Second.ToString(), 2) +
                        Right("00" + now.Millisecond.ToString(), 2);
                    dwABPOS_EXT["ABPEASTK"] = nQty;
                    if (SetExt.Tables.Contains("ABPOS_EXT"))
                    {
                        SetExt.Tables["ABPOS_EXT"].Clear();
                        dwData = SetExt.Tables["ABPOS_EXT"].NewRow();
                        dwData.ItemArray = dwABPOS_EXT.ItemArray;
                        SetExt.Tables["ABPOS_EXT"].Rows.Add(dwData);
                    }
                    else
                    {
                        DataTable tb = SetJob.Tables["ABPOS_EXT"].Clone();
                        dwData = tb.NewRow();
                        dwData.ItemArray = dwABPOS_EXT.ItemArray;
                        tb.Rows.Add(dwData);
                        SetExt.Merge(tb);
                    }

                    time = Right("00" + System.DateTime.Now.Hour, 2).ToString() + Right("00" + System.DateTime.Now.Minute, 2).ToString();
                    WLFDNR = 1;
                    //ABPZLO	ABPZLB	ABPBLO
                    XFBLO = dwABPOS_EXT["ABPZLO"].ToString();
                    XFBLB = dwABPOS_EXT["ABPZLB"].ToString();
                    XFBLP = Left(dwABPOS_EXT["ABPVFAUFLFD"].ToString() + "                         ", 8);

                    #endregion
                    IN_ABPOS(dwABPOS_EXT, nQty);
                    var qab = from DataRow row in Set4ITX.Tables["ABPOS"].AsEnumerable()
                              where row["ABPAUFKRS"].ToString() == krs
                              && row["ABPAUFLFD"].ToString() == lfd
                              && row["ABPAUFPOS"].ToString() == pos
                              select row;
                    foreach (DataRow row in qab)
                    {
                        dwABPOS = row;
                    }
                    if (dwABPOS == null)
                    {
                        continue;
                    }
                    IN_FCHAR(dwABPOS, nQty);
                    IN_ABAUFM(dwABPOS, nQty);
                    OUT_FBST(dwABPOS, nQty);
                    IN_ABFOLGE(dwABPOS, nQty);
                    if (PRLO == " ")
                    {
                        IsSuccess = false;
                        message += "外发厂需要指定，请联系电脑部或计划部，指定外发厂!";
                        INTEXBL.CheckEvents("A003020", dwABPOS, message, null);
                        throw new Exception(message);
                    }
                    EventDispatcher.Instance.RaiseStatus("开始核对面料" + krs + lfd, StatusEventType.RunningProgress);
                    CWBBST(dwABPOS, nQty);
                   
                    EventDispatcher.Instance.RaiseStatus("开始核对辅料" + krs + lfd, StatusEventType.RunningProgress);
                    CFBST(dwABPOS, nQty);
                    Upload2Intex();
                }
                catch (Exception ex)
                {
                    Tracer.Error("上传款号" + ART + "颜色" + Color + "遇到问题:", ex);
                    EventDispatcher.Instance.RaiseStatus("异常退出" + krs + lfd, StatusEventType.StopProgress);
                    //EventDispatcher.Instance.RaiseStatus("郁闷:" + ex.Message, StatusEventType.StopProgress);
                }
            }
            return Return;
        }
        public DataSet Repair(DataRow dwABPOS_EXT)
        {

            DataSet Result = new DataSet();
            DataSet Return = new DataSet();//is used to magage the data to upload to intex 
            string krs = "", lfd = "", pos = "";

            try
            {
                Set4ITX.Clear();
                SetExt.Clear();
                SetExt.Merge(INTEXDB);
                SetExt.Merge(SetMap);
                Convert2Intex = new Convert2Intex(SetExt, dwIntexStd, OracleDB, proxy);
                dwIntexStd = Convert2Intex.dwIntexStd;
                dwIntexStd["ITXUSER"] = UserID;
                dwIntexStd["ITXDATE"] = date;
                #region======Prepare=========
                INTEXDBC.SaveICC_ABPOS_EXT(dwABPOS_EXT);
                DataRow dwData = null;
                DataRow dwABPOS = null;
                krs = dwABPOS_EXT["ABPAUFKRS"].ToString();
                lfd = dwABPOS_EXT["ABPAUFLFD"].ToString();
                pos = dwABPOS_EXT["ABPAUFPOS"].ToString();
                dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAMANDANT"] = dwABPOS_EXT["ABPMANDANT"];
                dwData["ABAVKAUFKRS"] = krs;
                dwData["ABAVKAUFLFD"] = lfd;
                dwData["ABAVKAUFPOS"] = pos;
                Result = INTEXDBC.QueryEKPOS(dwData, 4);
                foreach (DataRow row in Result.Tables[0].Rows)
                {
                    decimal EKPME1 = Convert.ToDecimal(row["EKPME1"].ToString());
                    decimal EKPME3 = Convert.ToDecimal(row["EKPME3"].ToString());
                    if (EKPME1 - EKPME3 > 0)
                    {
                        IsSuccess = false;
                        message += krs + lfd + "-" + row["EKPAUFKRS"].ToString() + "-" + Helper.GetSubRollNum(row["EKPAUFLFD"].ToString()) + "," +
                            row["ABAART"].ToString().Trim() + "-" + row["ABAFARBE"].ToString().Trim() + "-" + row["ABAGROE"].ToString().Trim() +
                             ":,请入库!";
                        throw new Exception(message);
                    }
                }
                decimal nQty = System.Convert.ToDecimal(dwABPOS_EXT["ABPEAKG"]);
                if (nQty == 0)
                {
                    if (dwABPOS_EXT["ABPERLDAT"].ToString() == date)
                    {
                        INTEXDBC.UpdateABPOS(dwABPOS_EXT);
                    }
                    return Return;
                }
                DateTime now = System.DateTime.Now;
                WBEWBUCHUHR =
                    Right("00" + now.Hour.ToString(), 2) +
                    Right("00" + now.Minute.ToString(), 2) +
                    Right("00" + now.Second.ToString(), 2) +
                    Right("00" + now.Millisecond.ToString(), 2);
                dwABPOS_EXT["ABPEASTK"] = nQty;
                if (SetExt.Tables.Contains("ABPOS_EXT"))
                {
                    SetExt.Tables["ABPOS_EXT"].Clear();
                    dwData = SetExt.Tables["ABPOS_EXT"].NewRow();
                    dwData.ItemArray = dwABPOS_EXT.ItemArray;
                    SetExt.Tables["ABPOS_EXT"].Rows.Add(dwData);
                }
                else
                {
                    DataTable tb = SetJob.Tables["ABPOS_EXT"].Clone();
                    dwData = tb.NewRow();
                    dwData.ItemArray = dwABPOS_EXT.ItemArray;
                    tb.Rows.Add(dwData);
                    SetExt.Merge(tb);
                }

                time = Right("00" + System.DateTime.Now.Hour, 2).ToString() + Right("00" + System.DateTime.Now.Minute, 2).ToString();
                WLFDNR = 1;
                //ABPZLO	ABPZLB	ABPBLO
                XFBLO = dwABPOS_EXT["ABPZLO"].ToString();
                XFBLB = dwABPOS_EXT["ABPZLB"].ToString();
                XFBLP = Left(dwABPOS_EXT["ABPVFAUFLFD"].ToString() + "                         ", 8);

                #endregion
                GetABPOS(dwABPOS_EXT, nQty);
                var qab = from DataRow row in Set4ITX.Tables["ABPOS"].AsEnumerable()
                          where row["ABPAUFKRS"].ToString() == krs
                          && row["ABPAUFLFD"].ToString() == lfd
                          && row["ABPAUFPOS"].ToString() == pos
                          select row;
                foreach (DataRow row in qab)
                {
                    dwABPOS = row;
                }
                if (dwABPOS == null)
                {
                    return Return;
                }
                GetFCHAR(dwABPOS, nQty);
                GetABFOLGE(dwABPOS, nQty);
                if (PRLO == " ")
                {
                    IsSuccess = false;
                    message += "外发厂需要指定，请联系电脑部或计划部，指定外发厂!";
                    throw new Exception(message);
                }

                CWBBST(dwABPOS, nQty);
                CFBST(dwABPOS, nQty);
                Upload2Intex();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                //EventDispatcher.Instance.RaiseStatus("郁闷:" + ex.Message, StatusEventType.StopProgress);
                message = "";
            }
            return Return;
        }

        #endregion
        #region======Process====================
        private void IN_ABPOS(DataRow dwInput, decimal nqty)
        {
            DataRow dwData = INTEXDB.ABPOS.NewRow();
            dwData["ABPMANDANT"] = dwInput["ABPMANDANT"];
            dwData["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
            dwData["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
            dwData["ABPAUFPOS"] = dwInput["ABPAUFPOS"];
            DataSet SResult = INTEXDBC.QueryABPOS(dwData, 1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                decimal qty = System.Convert.ToDecimal(row["ABPSOLLMENGE7"].ToString());
                decimal aqty = System.Convert.ToDecimal(row["ABPEASTK"].ToString());
                decimal eqty = System.Convert.ToDecimal(row["ABPEALFM"].ToString());
                row["ABPEASTK"] = aqty + nqty;
                row["ABPEALFM"] = eqty + System.Convert.ToDecimal(nqty) * 100;
                row["ABPAENDDAT"] = date;
                row["ABPAENDBEN"] = UserID;
                /*if (qty <= eqty + System.Convert.ToDecimal(nqty) * 100)
                {
                    row["ABPERLKZ"] = "J";
                }
                row["ABPERLKZ"] = UserID;*/
                row["ABPERLKZ"] = dwInput["ABPERLKZ"];
                row["ABPERLDAT"] = dwInput["ABPERLDAT"];
                //dwABPOS["ABPERLDAT"] = m_Data.date;
            }
            Set4ITX.Merge(SResult);
            /*  foreach (DataRow row in Set4ITX.Tables["ABPOS"].Rows)
     {
         dwABPOS = row;
     }      
   
     if (SetExt.Tables.Contains("ABPOS_EXT"))
     {
         SetExt.Tables["ABPOS_EXT"].Clear();
     }
     DataTable tb = Set4ITX.Tables["ABPOS"].Copy();
     tb.TableName = "ABPOS_EXT";
     SetExt.Merge(tb);*/

        }
        private void GetABPOS(DataRow dwInput, decimal nqty)
        {
            DataRow dwData = INTEXDB.ABPOS.NewRow();
            dwData["ABPMANDANT"] = dwInput["ABPMANDANT"];
            dwData["ABPAUFKRS"] = dwInput["ABPAUFKRS"];
            dwData["ABPAUFLFD"] = dwInput["ABPAUFLFD"];
            dwData["ABPAUFPOS"] = dwInput["ABPAUFPOS"];
            DataSet SResult = INTEXDBC.QueryABPOS(dwData, 1);
            Set4ITX.Merge(SResult);
            /*  foreach (DataRow row in Set4ITX.Tables["ABPOS"].Rows)
     {
         dwABPOS = row;
     }      
   
     if (SetExt.Tables.Contains("ABPOS_EXT"))
     {
         SetExt.Tables["ABPOS_EXT"].Clear();
     }
     DataTable tb = Set4ITX.Tables["ABPOS"].Copy();
     tb.TableName = "ABPOS_EXT";
     SetExt.Merge(tb);*/

        }
        private void IN_ABFOLGE(DataRow dwABPOS, decimal nqty)
        {

            DataSet SResult = null;
            DataRow dwData = INTEXDB.ABFOLGE.NewRow();
            dwData["ABFMANDANT"] = dwABPOS["ABPMANDANT"];
            dwData["ABFAUFKRS"] = dwABPOS["ABPAUFKRS"];
            dwData["ABFAUFLFD"] = dwABPOS["ABPAUFLFD"];
            dwData["ABFAUFPOS"] = dwABPOS["ABPAUFPOS"];
            SResult = INTEXDBC.QueryABFOLGE(dwData, 1);
            foreach (DataRow dwABFOLGE in SResult.Tables[0].Rows)
            {
                decimal ABFISTK = System.Convert.ToDecimal(dwABFOLGE["ABFISTK"].ToString());
                dwABFOLGE["ABFISTK"] = ABFISTK + nqty;
                decimal ABFIMG = System.Convert.ToDecimal(dwABFOLGE["ABFIMG"].ToString());
                dwABFOLGE["ABFIMG"] = ABFIMG + nqty * 1000;
                dwABFOLGE["ABFRAFOLGE"] = dwABFOLGE["ABFAFOLGE"];
                decimal ABFIMTR = System.Convert.ToDecimal(dwABFOLGE["ABFIMTR"].ToString());
                dwABFOLGE["ABFIMTR"] = System.Convert.ToDecimal(dwABPOS["ABPZUGET"].ToString()) * 10;
                //                ABFISTARTDAT	ABFISTARTZT

                dwABFOLGE["ABFRUECKBEN"] = UserID;
                dwABFOLGE["ABFRUECKDAT"] = date;
                dwABFOLGE["ABFRUECKZT"] = time;
                dwABFOLGE["ABFAENDBEN"] = UserID;
                dwABFOLGE["ABFAENDDAT"] = date;
                PRLO = dwABFOLGE["ABFLO"].ToString();
                PRLB = dwABFOLGE["ABFLB"].ToString();
                PRLP = dwABFOLGE["ABFLP"].ToString();
                int ABFAENDZL = System.Convert.ToInt16(dwABFOLGE["ABFAENDZL"].ToString());
                dwABFOLGE["ABFAENDZL"] = ABFAENDZL + 1;
                IN_KSLEIST(dwABPOS, dwABFOLGE);
            }
            Set4ITX.Merge(SResult);

        }
        private void GetABFOLGE(DataRow dwABPOS, decimal nqty)
        {

            DataSet SResult = null;
            DataRow dwData = INTEXDB.ABFOLGE.NewRow();
            dwData["ABFMANDANT"] = dwABPOS["ABPMANDANT"];
            dwData["ABFAUFKRS"] = dwABPOS["ABPAUFKRS"];
            dwData["ABFAUFLFD"] = dwABPOS["ABPAUFLFD"];
            dwData["ABFAUFPOS"] = dwABPOS["ABPAUFPOS"];
            SResult = INTEXDBC.QueryABFOLGE(dwData, 1);
            foreach (DataRow dwABFOLGE in SResult.Tables[0].Rows)
            {
                PRLO = dwABFOLGE["ABFLO"].ToString();
                PRLB = dwABFOLGE["ABFLB"].ToString();
                PRLP = dwABFOLGE["ABFLP"].ToString();
            }
            Set4ITX.Merge(SResult);

        }
        private void IN_KSLEIST(DataRow dwABPOS, DataRow dwABFOLGE)
        {
            DataSet SResult = null;
            SResult = Convert2KSLEIST(dwABFOLGE);
            //WBEWBELEG0000038522014    1A                                    ;
            Set4ITX.Merge(SResult);
        }
        private void IN_FCHAR(DataRow dwABPOS, decimal nqty)
        {
            DataSet SResult = null;
            DataRow dwData = INTEXDB.FCHAR.NewRow();
            dwData["FCMANDANT"] = dwABPOS["ABPMANDANT"];
            dwData["FCAUFTYP"] = "V";
            dwData["FCBESTKRS"] = dwABPOS["ABPAUFKRS"];
            dwData["FCBESTLFD"] = dwABPOS["ABPAUFLFD"];
            dwData["FCBESTPOS"] = dwABPOS["ABPAUFPOS"];
            SResult = INTEXDBC.QueryFCHAR(dwData, 1);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                SResult = Convert2FCHAR(dwABPOS);
            }
            foreach (DataRow dwFCHAR in SResult.Tables[0].Rows)
            {
                decimal FCME = System.Convert.ToDecimal(dwFCHAR["FCME"].ToString());
                dwFCHAR["FCME"] = FCME + nqty;
                decimal FCEME = System.Convert.ToDecimal(dwFCHAR["FCEME"].ToString());
                dwFCHAR["FCEME"] = FCEME + nqty;
                FCCHAR = dwFCHAR["FCCHAR"].ToString();
            }
            Set4ITX.Merge(SResult);

            if (SetExt.Tables.Contains("FCHAR_EXT"))
            {
                SetExt.Tables.Remove("FCHAR_EXT");
            }
            DataTable tb = Set4ITX.Tables["FCHAR"].Copy();
            tb.TableName = "FCHAR_EXT";
            SetExt.Merge(tb);

        }
        private void GetFCHAR(DataRow dwABPOS, decimal nqty)
        {
            DataSet SResult = null;
            DataRow dwData = INTEXDB.FCHAR.NewRow();
            dwData["FCMANDANT"] = dwABPOS["ABPMANDANT"];
            dwData["FCAUFTYP"] = "V";
            dwData["FCBESTKRS"] = dwABPOS["ABPAUFKRS"];
            dwData["FCBESTLFD"] = dwABPOS["ABPAUFLFD"];
            dwData["FCBESTPOS"] = dwABPOS["ABPAUFPOS"];
            SResult = INTEXDBC.QueryFCHAR(dwData, 1);
            Set4ITX.Merge(SResult);

            if (SetExt.Tables.Contains("FCHAR_EXT"))
            {
                SetExt.Tables.Remove("FCHAR_EXT");
            }
            DataTable tb = Set4ITX.Tables["FCHAR"].Copy();
            tb.TableName = "FCHAR_EXT";
            SetExt.Merge(tb);

        }
        private void IN_ABAUFM(DataRow dwABPOS, decimal nqty)
        {
            DataSet SResult = null;
            DataRow dwData = INTEXDB.ABAUFM.NewRow();
            dwData["ABAMANDANT"] = dwABPOS["ABPMANDANT"];
            dwData["ABAAUFKRS"] = dwABPOS["ABPAUFKRS"];
            dwData["ABAAUFLFD"] = dwABPOS["ABPAUFLFD"];
            dwData["ABAAUFPOS"] = dwABPOS["ABPAUFPOS"];
            SResult = INTEXDBC.QueryABAUFM(dwData, 5);
            foreach (DataRow dwABAUFM in SResult.Tables[0].Rows)
            {
                decimal ABAZUSTK = System.Convert.ToDecimal(dwABAUFM["ABAZUSTK"].ToString());
                dwABAUFM["ABAZUSTK"] = ABAZUSTK + nqty;
                decimal ABAIANZSTK = System.Convert.ToDecimal(dwABAUFM["ABAIANZSTK"].ToString());
                dwABAUFM["ABAIANZSTK"] = ABAIANZSTK + nqty;
                dwABAUFM["ABAIZENSUR1"] = "1";
                dwABAUFM["ABAIZENSURKZ1"] = "A";

                decimal ABAIGESLGZS1 = System.Convert.ToDecimal(dwABAUFM["ABAIGESLGZS1"].ToString());
                dwABAUFM["ABAIGESLGZS1"] = ABAIGESLGZS1 + nqty * 1000;

                decimal ABAZULG = System.Convert.ToDecimal(dwABAUFM["ABAZULG"].ToString());
                dwABAUFM["ABAZULG"] = ABAZULG + nqty * 1000;
                dwABAUFM["ABAPARTIE"] = FCCHAR;
                dwABAUFM["ABAAENDBEN"] = UserID;
                dwABAUFM["ABAAENDDAT"] = date;
            }
            Set4ITX.Merge(SResult);

        }
        private void OUT_FBST(DataRow dwABPOS, decimal nqty)
        {

            DataSet SResult = null;
            DataRow dwData = INTEXDB.FBST.NewRow();
            dwData["FBMANDANT"] = dwABPOS["ABPMANDANT"];
            dwData["FBCHAR"] = FCCHAR;
            dwData["FBLO"] = XFBLO;
            dwData["FBLB"] = XFBLB;
            dwData["FBLP"] = XFBLP;
            SResult = INTEXDBC.QueryFBST(dwData, 3);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                SResult = Convert2FBST(dwABPOS);
            }
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                decimal instock = System.Convert.ToDecimal(row["FBME"].ToString());
                row["FBME"] = instock + nqty;
                OUT_FGBST(dwABPOS, row, nqty);
                OUT_FBEW(dwABPOS, row, nqty);
            }
            Set4ITX.Merge(SResult);
            /*
            if (SetExt.Tables.Contains("FBST_EXT"))
            {
                SetExt.Tables["FBST_EXT"].Clear();
            }
            DataTable tb = Set4ITX.Tables["FBST"].Clone();
            tb.TableName = "FBST_EXT";
            SetExt.Merge(tb);*/

        }
        private void OUT_FGBST(DataRow dwABPOS, DataRow dwFBST, decimal nqty)
        {
            DataSet SResult = null;
            DataRow dwData = INTEXDB.FGBST.NewRow();
            dwData["FGMANDANT"] = dwFBST["FBMANDANT"];
            dwData["FGART"] = dwFBST["FBART"];
            dwData["FGVAR"] = dwFBST["FBVAR"];
            dwData["FGFARBE"] = dwFBST["FBFARBE"];
            dwData["FGGROE"] = dwFBST["FBGROE"];
            dwData["FGLO"] = dwFBST["FBLO"];
            dwData["FGMJJ"] = year;
            dwData["FGMM"] = month;
            SResult = INTEXDBC.QueryFGBST(dwData, 1);
            if (SResult.Tables[0].Rows.Count == 0)
            {
                SResult = Convert2FGBST(dwABPOS);
            }

            foreach (DataRow dwFGBST in SResult.Tables["FGBST"].Rows)
            {
                dwFGBST["FGDLABST"] = dwFGBST["FGBSTA"];
                decimal FGME13 = System.Convert.ToDecimal(dwFGBST["FGME13"].ToString());
                dwFGBST["FGME13"] = FGME13 + nqty;
                dwFGBST["FGRE01"] = dwFGBST["FGME13"];
                decimal FGBSTA = System.Convert.ToDecimal(dwFGBST["FGBSTA"].ToString());
                dwFGBST["FGBSTA"] = FGBSTA + nqty;
                decimal FGBSTME = System.Convert.ToDecimal(dwFGBST["FGBSTME"].ToString());

                dwFGBST["FGBSTME"] = FGBSTME + nqty;
                decimal FGMAXBST = System.Convert.ToDecimal(dwFGBST["FGMAXBST"].ToString());
                dwFGBST["FGMAXBST"] = FGMAXBST + nqty;

                dwFGBST["FGAENDBEN"] = UserID;
                dwFGBST["FGAENDDAT"] = date;
            }
            Set4ITX.Merge(SResult);
            /*
            if (SetExt.Tables.Contains("FGBST_EXT"))
            {
                SetExt.Tables["FGBST_EXT"].Clear();
            }
            DataTable tb = Set4ITX.Tables["FGBST"].Clone();
            tb.TableName = "FGBST_EXT";
            SetExt.Merge(tb);*/
        }
        private void OUT_FBEW(DataRow dwABPOS, DataRow dwFBST, decimal nqty)
        {

            DataSet SResult = null;
            SResult = Convert2FBEW13(dwABPOS);
            Set4ITX.Merge(SResult);
        }
        #endregion

        public void AB2FB(SETINTEXDB src)
        {
            DataSet SetTmp = new DataSet();
            DataSet SReturn = new DataSet();
            string xml = @"C:\IntexReport\Data\YCAAV25\01Temp\AV" + DateTime.Now.ToFileTime().ToString() + ".XML";
            SetTmp.Clear();
            DataTable tbtemp = src.ABPOS.Clone();
            tbtemp.TableName = "ABPOS_EXT";
            DataRow dwTemp;
            foreach (DataRow row in src.ABPOS.Rows)
            {
                if (Convert.ToDecimal(row["ABPEAKG"].ToString()) == 0)
                {
                    if (row["ABPERLDAT"].ToString() != date)
                    {
                        continue;
                    }
                }
                dwTemp = tbtemp.NewRow();
                dwTemp.ItemArray = row.ItemArray;
                tbtemp.Rows.Add(dwTemp);
            }
            if (tbtemp.Rows.Count > 0)
            {
                SetTmp.Merge(tbtemp);
                SetTmp.WriteXml(xml);
            }
            SaveJob();
        }
    }
}
